In [1]:
import os
os.environ['KERAS_BACKEND']='tensorflow' # can choose theano, tensorflow, cntk
os.environ['THEANO_FLAGS']='floatX=float32,device=cuda,optimizer=fast_run,dnn.library_path=/usr/lib'

import keras.backend as K
K.set_image_data_format('channels_last')
channel_axis = -1
Using TensorFlow backend.
In [3]:
from keras.models import Sequential, Model
from keras.layers import Conv2D, ZeroPadding2D, BatchNormalization, Input, Dropout
from keras.layers import Conv2DTranspose, Reshape, Activation, Cropping2D, Flatten
from keras.layers import Concatenate
from keras.layers.advanced_activations import LeakyReLU
from keras.activations import relu
from keras.initializers import RandomNormal
In [4]:
# Weights initializations
# bias are initailized as 0
#conv 웨이트 초기화
def __conv_init(a):
    print("conv_init", a)
    k = RandomNormal(0, 0.02)(a) # for convolution kernel
    k.conv_weight = True    
    return k
conv_init = RandomNormal(0, 0.02)
gamma_init = RandomNormal(1., 0.02) # for batch normalization
In [5]:
# Basic discriminator
def conv2d(f, *a, **k):
    return Conv2D(f, kernel_initializer = conv_init, *a, **k)

def batchnorm():
    #batchnormalization
    return BatchNormalization(momentum=0.9, axis=channel_axis, epsilon=1.01e-5,
                                   gamma_initializer = gamma_init)
def BASIC_D(nc_in, nc_out, ndf, max_layers=3):
    """DCGAN_D(nc, ndf, max_layers=3)
       nc: channels
       ndf: filters of the first layer
       max_layers: max hidden layers
    """    
    
    input_a, input_b = Input(shape=(None, None, nc_in)), Input(shape=(None, None, nc_out))
    #두개의 인풋을 concat
    _ = Concatenate(axis=channel_axis)([input_a, input_b])
    #CNN Layer
    _ = conv2d(ndf, kernel_size=4, strides=2, padding="same", name = 'First') (_)
    _ = LeakyReLU(alpha=0.2)(_)
    #layer를 max_layer 만큼 중첩해서 쌓음
    for layer in range(1, max_layers):        
        out_feat = ndf * min(2**layer, 8)
        _ = conv2d(out_feat, kernel_size=4, strides=2, padding="same", 
                   use_bias=False, name = 'pyramid.{0}'.format(layer)             
                        ) (_)
        _ = batchnorm()(_, training=1)        
        _ = LeakyReLU(alpha=0.2)(_)
    
    out_feat = ndf*min(2**max_layers, 8)
    _ = ZeroPadding2D(1)(_)
    _ = conv2d(out_feat, kernel_size=4,  use_bias=False, name = 'pyramid_last') (_)
    _ = batchnorm()(_, training=1)
    _ = LeakyReLU(alpha=0.2)(_)
    
    # final layer
    _ = ZeroPadding2D(1)(_)
    _ = conv2d(1, kernel_size=4, name = 'final'.format(out_feat, 1), 
               activation = "sigmoid") (_)    
    return Model(inputs=[input_a, input_b], outputs=_)
In [6]:
def UNET_G(isize, nc_in=3, nc_out=3, ngf=64, fixed_input_size=True):    
    max_nf = 8*ngf    
    def block(x, s, nf_in, use_batchnorm=True, nf_out=None, nf_next=None):
        print("block",x,s,nf_in, use_batchnorm, nf_out, nf_next)
        assert s>=2 and s%2==0
        if nf_next is None:
            nf_next = min(nf_in*2, max_nf)
        if nf_out is None:
            nf_out = nf_in
        x = conv2d(nf_next, kernel_size=4, strides=2, use_bias=(not (use_batchnorm and s>2)),
                   padding="same", name = 'conv_{0}'.format(s)) (x)
        if s>2:
            if use_batchnorm:
                x = batchnorm()(x, training=1)
            x2 = LeakyReLU(alpha=0.2)(x)
            x2 = block(x2, s//2, nf_next)
            x = Concatenate(axis=channel_axis)([x, x2])            
        x = Activation("relu")(x)
        x = Conv2DTranspose(nf_out, kernel_size=4, strides=2, use_bias=not use_batchnorm,
                            kernel_initializer = conv_init,          
                            name = 'convt.{0}'.format(s))(x)        
        x = Cropping2D(1)(x)
        if use_batchnorm:
            x = batchnorm()(x, training=1)
        if s <=8:
            x = Dropout(0.5)(x, training=1)
        return x
    
    s = isize if fixed_input_size else None #image size
    _ = inputs = Input(shape=(s, s, nc_in)) #input layer
    _ = block(_, isize, nc_in, False, nf_out=nc_out, nf_next=ngf)
    _ = Activation('tanh')(_)

    return Model(inputs=inputs, outputs=[_])
In [7]:
nc_in = 3 #채널 수
nc_out = 3
ngf = 64 #필터 수
ndf = 64
lamda = 10

loadSize = 286
imageSize = 256
batchSize = 1
lrD = 2e-4
lrG = 2e-4
In [8]:
netD = BASIC_D(nc_in, nc_out, ndf)
netD.summary()
__________________________________________________________________________________________________
Layer (type)                    Output Shape         Param #     Connected to                     
==================================================================================================
input_1 (InputLayer)            (None, None, None, 3 0                                            
__________________________________________________________________________________________________
input_2 (InputLayer)            (None, None, None, 3 0                                            
__________________________________________________________________________________________________
concatenate_1 (Concatenate)     (None, None, None, 6 0           input_1[0][0]                    
                                                                 input_2[0][0]                    
__________________________________________________________________________________________________
First (Conv2D)                  (None, None, None, 6 6208        concatenate_1[0][0]              
__________________________________________________________________________________________________
leaky_re_lu_1 (LeakyReLU)       (None, None, None, 6 0           First[0][0]                      
__________________________________________________________________________________________________
pyramid.1 (Conv2D)              (None, None, None, 1 131072      leaky_re_lu_1[0][0]              
__________________________________________________________________________________________________
batch_normalization_1 (BatchNor (None, None, None, 1 512         pyramid.1[0][0]                  
__________________________________________________________________________________________________
leaky_re_lu_2 (LeakyReLU)       (None, None, None, 1 0           batch_normalization_1[0][0]      
__________________________________________________________________________________________________
pyramid.2 (Conv2D)              (None, None, None, 2 524288      leaky_re_lu_2[0][0]              
__________________________________________________________________________________________________
batch_normalization_2 (BatchNor (None, None, None, 2 1024        pyramid.2[0][0]                  
__________________________________________________________________________________________________
leaky_re_lu_3 (LeakyReLU)       (None, None, None, 2 0           batch_normalization_2[0][0]      
__________________________________________________________________________________________________
zero_padding2d_1 (ZeroPadding2D (None, None, None, 2 0           leaky_re_lu_3[0][0]              
__________________________________________________________________________________________________
pyramid_last (Conv2D)           (None, None, None, 5 2097152     zero_padding2d_1[0][0]           
__________________________________________________________________________________________________
batch_normalization_3 (BatchNor (None, None, None, 5 2048        pyramid_last[0][0]               
__________________________________________________________________________________________________
leaky_re_lu_4 (LeakyReLU)       (None, None, None, 5 0           batch_normalization_3[0][0]      
__________________________________________________________________________________________________
zero_padding2d_2 (ZeroPadding2D (None, None, None, 5 0           leaky_re_lu_4[0][0]              
__________________________________________________________________________________________________
final (Conv2D)                  (None, None, None, 1 8193        zero_padding2d_2[0][0]           
==================================================================================================
Total params: 2,770,497
Trainable params: 2,768,705
Non-trainable params: 1,792
__________________________________________________________________________________________________
In [9]:
from IPython.display import SVG
from keras.utils.vis_utils import model_to_dot

netG = UNET_G(imageSize, nc_in, nc_out, ngf)
SVG(model_to_dot(netG, show_shapes=True).create(prog='dot', format='svg'))
netG.summary()
1 들어오고
2 나가고
block Tensor("input_3:0", shape=(?, 256, 256, 3), dtype=float32) 256 3 False 3 64
block Tensor("leaky_re_lu_5/sub:0", shape=(?, 128, 128, 64), dtype=float32) 128 64 True None None
block Tensor("leaky_re_lu_6/sub:0", shape=(?, 64, 64, 128), dtype=float32) 64 128 True None None
block Tensor("leaky_re_lu_7/sub:0", shape=(?, 32, 32, 256), dtype=float32) 32 256 True None None
block Tensor("leaky_re_lu_8/sub:0", shape=(?, 16, 16, 512), dtype=float32) 16 512 True None None
block Tensor("leaky_re_lu_9/sub:0", shape=(?, 8, 8, 512), dtype=float32) 8 512 True None None
block Tensor("leaky_re_lu_10/sub:0", shape=(?, 4, 4, 512), dtype=float32) 4 512 True None None
block Tensor("leaky_re_lu_11/sub:0", shape=(?, 2, 2, 512), dtype=float32) 2 512 True None None
__________________________________________________________________________________________________
Layer (type)                    Output Shape         Param #     Connected to                     
==================================================================================================
input_3 (InputLayer)            (None, 256, 256, 3)  0                                            
__________________________________________________________________________________________________
conv_256 (Conv2D)               (None, 128, 128, 64) 3136        input_3[0][0]                    
__________________________________________________________________________________________________
leaky_re_lu_5 (LeakyReLU)       (None, 128, 128, 64) 0           conv_256[0][0]                   
__________________________________________________________________________________________________
conv_128 (Conv2D)               (None, 64, 64, 128)  131072      leaky_re_lu_5[0][0]              
__________________________________________________________________________________________________
batch_normalization_4 (BatchNor (None, 64, 64, 128)  512         conv_128[0][0]                   
__________________________________________________________________________________________________
leaky_re_lu_6 (LeakyReLU)       (None, 64, 64, 128)  0           batch_normalization_4[0][0]      
__________________________________________________________________________________________________
conv_64 (Conv2D)                (None, 32, 32, 256)  524288      leaky_re_lu_6[0][0]              
__________________________________________________________________________________________________
batch_normalization_5 (BatchNor (None, 32, 32, 256)  1024        conv_64[0][0]                    
__________________________________________________________________________________________________
leaky_re_lu_7 (LeakyReLU)       (None, 32, 32, 256)  0           batch_normalization_5[0][0]      
__________________________________________________________________________________________________
conv_32 (Conv2D)                (None, 16, 16, 512)  2097152     leaky_re_lu_7[0][0]              
__________________________________________________________________________________________________
batch_normalization_6 (BatchNor (None, 16, 16, 512)  2048        conv_32[0][0]                    
__________________________________________________________________________________________________
leaky_re_lu_8 (LeakyReLU)       (None, 16, 16, 512)  0           batch_normalization_6[0][0]      
__________________________________________________________________________________________________
conv_16 (Conv2D)                (None, 8, 8, 512)    4194304     leaky_re_lu_8[0][0]              
__________________________________________________________________________________________________
batch_normalization_7 (BatchNor (None, 8, 8, 512)    2048        conv_16[0][0]                    
__________________________________________________________________________________________________
leaky_re_lu_9 (LeakyReLU)       (None, 8, 8, 512)    0           batch_normalization_7[0][0]      
__________________________________________________________________________________________________
conv_8 (Conv2D)                 (None, 4, 4, 512)    4194304     leaky_re_lu_9[0][0]              
__________________________________________________________________________________________________
batch_normalization_8 (BatchNor (None, 4, 4, 512)    2048        conv_8[0][0]                     
__________________________________________________________________________________________________
leaky_re_lu_10 (LeakyReLU)      (None, 4, 4, 512)    0           batch_normalization_8[0][0]      
__________________________________________________________________________________________________
conv_4 (Conv2D)                 (None, 2, 2, 512)    4194304     leaky_re_lu_10[0][0]             
__________________________________________________________________________________________________
batch_normalization_9 (BatchNor (None, 2, 2, 512)    2048        conv_4[0][0]                     
__________________________________________________________________________________________________
leaky_re_lu_11 (LeakyReLU)      (None, 2, 2, 512)    0           batch_normalization_9[0][0]      
__________________________________________________________________________________________________
conv_2 (Conv2D)                 (None, 1, 1, 512)    4194816     leaky_re_lu_11[0][0]             
__________________________________________________________________________________________________
activation_1 (Activation)       (None, 1, 1, 512)    0           conv_2[0][0]                     
__________________________________________________________________________________________________
convt.2 (Conv2DTranspose)       (None, 4, 4, 512)    4194304     activation_1[0][0]               
__________________________________________________________________________________________________
cropping2d_1 (Cropping2D)       (None, 2, 2, 512)    0           convt.2[0][0]                    
__________________________________________________________________________________________________
batch_normalization_10 (BatchNo (None, 2, 2, 512)    2048        cropping2d_1[0][0]               
__________________________________________________________________________________________________
dropout_1 (Dropout)             (None, 2, 2, 512)    0           batch_normalization_10[0][0]     
__________________________________________________________________________________________________
concatenate_2 (Concatenate)     (None, 2, 2, 1024)   0           batch_normalization_9[0][0]      
                                                                 dropout_1[0][0]                  
__________________________________________________________________________________________________
activation_2 (Activation)       (None, 2, 2, 1024)   0           concatenate_2[0][0]              
__________________________________________________________________________________________________
convt.4 (Conv2DTranspose)       (None, 6, 6, 512)    8388608     activation_2[0][0]               
__________________________________________________________________________________________________
cropping2d_2 (Cropping2D)       (None, 4, 4, 512)    0           convt.4[0][0]                    
__________________________________________________________________________________________________
batch_normalization_11 (BatchNo (None, 4, 4, 512)    2048        cropping2d_2[0][0]               
__________________________________________________________________________________________________
dropout_2 (Dropout)             (None, 4, 4, 512)    0           batch_normalization_11[0][0]     
__________________________________________________________________________________________________
concatenate_3 (Concatenate)     (None, 4, 4, 1024)   0           batch_normalization_8[0][0]      
                                                                 dropout_2[0][0]                  
__________________________________________________________________________________________________
activation_3 (Activation)       (None, 4, 4, 1024)   0           concatenate_3[0][0]              
__________________________________________________________________________________________________
convt.8 (Conv2DTranspose)       (None, 10, 10, 512)  8388608     activation_3[0][0]               
__________________________________________________________________________________________________
cropping2d_3 (Cropping2D)       (None, 8, 8, 512)    0           convt.8[0][0]                    
__________________________________________________________________________________________________
batch_normalization_12 (BatchNo (None, 8, 8, 512)    2048        cropping2d_3[0][0]               
__________________________________________________________________________________________________
dropout_3 (Dropout)             (None, 8, 8, 512)    0           batch_normalization_12[0][0]     
__________________________________________________________________________________________________
concatenate_4 (Concatenate)     (None, 8, 8, 1024)   0           batch_normalization_7[0][0]      
                                                                 dropout_3[0][0]                  
__________________________________________________________________________________________________
activation_4 (Activation)       (None, 8, 8, 1024)   0           concatenate_4[0][0]              
__________________________________________________________________________________________________
convt.16 (Conv2DTranspose)      (None, 18, 18, 512)  8388608     activation_4[0][0]               
__________________________________________________________________________________________________
cropping2d_4 (Cropping2D)       (None, 16, 16, 512)  0           convt.16[0][0]                   
__________________________________________________________________________________________________
batch_normalization_13 (BatchNo (None, 16, 16, 512)  2048        cropping2d_4[0][0]               
__________________________________________________________________________________________________
concatenate_5 (Concatenate)     (None, 16, 16, 1024) 0           batch_normalization_6[0][0]      
                                                                 batch_normalization_13[0][0]     
__________________________________________________________________________________________________
activation_5 (Activation)       (None, 16, 16, 1024) 0           concatenate_5[0][0]              
__________________________________________________________________________________________________
convt.32 (Conv2DTranspose)      (None, 34, 34, 256)  4194304     activation_5[0][0]               
__________________________________________________________________________________________________
cropping2d_5 (Cropping2D)       (None, 32, 32, 256)  0           convt.32[0][0]                   
__________________________________________________________________________________________________
batch_normalization_14 (BatchNo (None, 32, 32, 256)  1024        cropping2d_5[0][0]               
__________________________________________________________________________________________________
concatenate_6 (Concatenate)     (None, 32, 32, 512)  0           batch_normalization_5[0][0]      
                                                                 batch_normalization_14[0][0]     
__________________________________________________________________________________________________
activation_6 (Activation)       (None, 32, 32, 512)  0           concatenate_6[0][0]              
__________________________________________________________________________________________________
convt.64 (Conv2DTranspose)      (None, 66, 66, 128)  1048576     activation_6[0][0]               
__________________________________________________________________________________________________
cropping2d_6 (Cropping2D)       (None, 64, 64, 128)  0           convt.64[0][0]                   
__________________________________________________________________________________________________
batch_normalization_15 (BatchNo (None, 64, 64, 128)  512         cropping2d_6[0][0]               
__________________________________________________________________________________________________
concatenate_7 (Concatenate)     (None, 64, 64, 256)  0           batch_normalization_4[0][0]      
                                                                 batch_normalization_15[0][0]     
__________________________________________________________________________________________________
activation_7 (Activation)       (None, 64, 64, 256)  0           concatenate_7[0][0]              
__________________________________________________________________________________________________
convt.128 (Conv2DTranspose)     (None, 130, 130, 64) 262144      activation_7[0][0]               
__________________________________________________________________________________________________
cropping2d_7 (Cropping2D)       (None, 128, 128, 64) 0           convt.128[0][0]                  
__________________________________________________________________________________________________
batch_normalization_16 (BatchNo (None, 128, 128, 64) 256         cropping2d_7[0][0]               
__________________________________________________________________________________________________
concatenate_8 (Concatenate)     (None, 128, 128, 128 0           conv_256[0][0]                   
                                                                 batch_normalization_16[0][0]     
__________________________________________________________________________________________________
activation_8 (Activation)       (None, 128, 128, 128 0           concatenate_8[0][0]              
__________________________________________________________________________________________________
convt.256 (Conv2DTranspose)     (None, 258, 258, 3)  6147        activation_8[0][0]               
__________________________________________________________________________________________________
cropping2d_8 (Cropping2D)       (None, 256, 256, 3)  0           convt.256[0][0]                  
__________________________________________________________________________________________________
activation_9 (Activation)       (None, 256, 256, 3)  0           cropping2d_8[0][0]               
==================================================================================================
Total params: 54,424,387
Trainable params: 54,414,531
Non-trainable params: 9,856
__________________________________________________________________________________________________
In [10]:
def extract_patches(images, sub_patch_dim):
    """
    Cuts images into k subpatches
    Each kth cut as the kth patches for all images
    ex: input 3 images [im1, im2, im3]
    output [[im_1_patch_1, im_2_patch_1], ... , [im_n-1_patch_k, im_n_patch_k]]
    :param images: array of Images (num_images, im_channels, im_height, im_width)
    :param sub_patch_dim: (height, width) ex: (30, 30) Subpatch dimensions
    :return:
    """
    im_height, im_width = images.shape[1:3]
    print(images.shape)
    patch_height, patch_width = sub_patch_dim

    # list out all xs  ex: 0, 29, 58, ...
    x_spots = range(0, im_width, patch_width)

    # list out all ys ex: 0, 29, 58
    y_spots = range(0, im_height, patch_height)
    all_patches = []

    for y in y_spots:
        for x in x_spots:
            # indexing here is cra
            # images[num_images, num_channels, width, height]
            # this says, cut a patch across all images at the same time with this width, height
            image_patches = images[:, y: y+patch_height, x: x+patch_width, :]
            all_patches.append(np.float32(image_patches))
    return all_patches
In [11]:
from keras.optimizers import RMSprop, SGD, Adam
In [ ]:
def tensor_mean(A):
    sum_tensor = 0
    for t in A:
        sum_tensor += t
    return (sum_tensor/len(A))
def make_patch_Tensor(A):
    patch_list = []
    for ptc in K.tf.split(A,4,axis=1):
        patch_list.extend(K.tf.split(ptc,4,axis=2))

    return patch_list
In [14]:
real_A = netG.input #real image
fake_B = netG.output #fake image
netG_generate = K.function([real_A], [fake_B])#img generator
real_B = K.tf.placeholder(dtype="float32",shape=(None,None,None,3)) #discriminator의 인풋

real_A_patches = make_patch_Tensor(real_A)
real_B_patches = make_patch_Tensor(real_B)
fake_B_patches = make_patch_Tensor(fake_B)

"""
output_D_real = netD([real_A, real_B]) #D의 real 아웃풋
output_D_fake = netD([real_A, fake_B]) #D의 fake 아웃풋
"""

output_D_real = [netD([a, b]) for a,b in zip(real_A_patches,real_B_patches)]
output_D_fake = [netD([a, b]) for a,b in zip(real_A_patches,fake_B_patches)]

output_D_real = tensor_mean(output_D_real)
output_D_fake = tensor_mean(output_D_fake)
In [15]:
loss_fn = lambda output, target : -K.mean(K.log(output+1e-12)*target+K.log(1-output+1e-12)*(1-target))

loss_D_real = loss_fn(output_D_real, K.ones_like(output_D_real))
loss_D_fake = loss_fn(output_D_fake, K.zeros_like(output_D_fake)) #가짜 분별 학습
loss_G_fake = loss_fn(output_D_fake, K.ones_like(output_D_fake)) #미스 Leading?

loss_L1 = K.mean(K.abs(fake_B-real_B))
In [16]:
loss_D = loss_D_real +loss_D_fake
training_updates = Adam(lr=lrD, beta_1=0.5).get_updates(netD.trainable_weights,[],loss_D)
netD_train = K.function([real_A, real_B],[loss_D/2.0], training_updates)
In [17]:
loss_G = loss_G_fake   + 100 * loss_L1
training_updates = Adam(lr=lrG, beta_1=0.5).get_updates(netG.trainable_weights,[], loss_G)
netG_train = K.function([real_A, real_B], [loss_G_fake, loss_L1], training_updates)
In [18]:
from PIL import Image
import numpy as np
import glob
from random import randint, shuffle

def load_data(file_pattern):
    return glob.glob(file_pattern)
# 두장이 concat된 사진을 두장으로 나누는 부분
def read_image(fn, direction=0):
    im = Image.open(fn)
    im = im.resize( (loadSize*2, loadSize), Image.BILINEAR )
    arr = np.array(im)/255.0*2-1
    w1,w2 = (loadSize-imageSize)//2,(loadSize+imageSize)//2
    h1,h2 = w1,w2
    
    imgA = arr[h1:h2, loadSize+w1:loadSize+w2, :]
    imgB = arr[h1:h2, w1:w2, :]

    if direction==0:
        return imgA, imgB
    else:
        return imgB,imgA

direction = 1

trainAB = load_data('./cards_ab/train/*.jpg')
valAB = load_data('./cards_ab/val/*.jpg')

assert len(trainAB) and len(valAB)
In [19]:
def minibatch(dataAB, batchsize, direction=0):
    length = len(dataAB)
    epoch = i = 0
    tmpsize = None    
    while True:
        size = tmpsize if tmpsize else batchsize #batchsize
        if i+size > length:
            shuffle(dataAB)
            i = 0
            epoch+=1        
        dataA = []
        dataB = []
        for j in range(i,i+size):
            imgA,imgB = read_image(dataAB[j], direction)#img 두 장을 받아서 list에 넣어줌
            dataA.append(imgA)
            dataB.append(imgB)
        dataA = np.float32(dataA)
        dataB = np.float32(dataB)
        i+=size
        tmpsize = yield epoch, dataA, dataB
In [20]:
from IPython.display import display
def showX(X, rows=1):
    assert X.shape[0]%rows == 0
    
    int_X = ( (X+1)/2.0*255).clip(0,255).astype('uint8')
    int_X = int_X.reshape(-1,imageSize,imageSize, 3)
    int_X = int_X.reshape(rows, -1, imageSize, imageSize,3).swapaxes(1,2).reshape(rows*imageSize,-1, 3)

    display(Image.fromarray(int_X))
In [21]:
train_batch = minibatch(trainAB, 6, direction=direction)
_, trainA, trainB = next(train_batch)
showX(trainA)
showX(trainB)
del train_batch, trainA, trainB
In [22]:
def netG_gen(A):
    return np.concatenate([netG_generate([A[i:i+1]])[0] for i in range(A.shape[0])], axis=0)
In [23]:
import time
from IPython.display import clear_output
t0 = time.time()
niter = 50 #epoch
gen_iterations = 0
errL1 = epoch = errG = 0
errL1_sum = errG_sum = errD_sum = 0

display_iters = 500
val_batch = minibatch(valAB, 6, direction)
train_batch = minibatch(trainAB, batchSize, direction)

while epoch < niter: 
    epoch, trainA, trainB = next(train_batch)        
    errD,  = netD_train([trainA, trainB])
    errD_sum +=errD

    errG, errL1 = netG_train([trainA, trainB])
    errG_sum += errG
    errL1_sum += errL1
    gen_iterations+=1
    if gen_iterations%display_iters==0:
        if gen_iterations%(5*display_iters)==0:
            clear_output()
        print('[%d/%d][%d] Loss_D: %f Loss_G: %f loss_L1: %f'
        % (epoch, niter, gen_iterations, errD_sum/display_iters, errG_sum/display_iters, errL1_sum/display_iters), time.time()-t0)
        _, valA, valB = train_batch.send(6) 
        fakeB = netG_gen(valA)
        showX(np.concatenate([valA, valB, fakeB], axis=0), 3)
        errL1_sum = errG_sum = errD_sum = 0
        _, valA, valB = next(val_batch)
        fakeB = netG_gen(valA)
        showX(np.concatenate([valA, valB, fakeB], axis=0), 3)
[45/50][45000] Loss_D: 0.294809 Loss_G: 1.707048 loss_L1: 0.027647 34143.171513319016
IOPub data rate exceeded.
The notebook server will temporarily stop sending output
to the client in order to avoid crashing it.
To change this limit, set the config variable
`--NotebookApp.iopub_data_rate_limit`.
[46/50][45500] Loss_D: 0.293194 Loss_G: 1.721450 loss_L1: 0.027604 34528.95465564728
IOPub data rate exceeded.
The notebook server will temporarily stop sending output
to the client in order to avoid crashing it.
To change this limit, set the config variable
`--NotebookApp.iopub_data_rate_limit`.
[46/50][46000] Loss_D: 0.294336 Loss_G: 1.666782 loss_L1: 0.027726 34914.41825675964
IOPub data rate exceeded.
The notebook server will temporarily stop sending output
to the client in order to avoid crashing it.
To change this limit, set the config variable
`--NotebookApp.iopub_data_rate_limit`.
---------------------------------------------------------------------------
KeyboardInterrupt                         Traceback (most recent call last)
<ipython-input-23-569c0df83cd6> in <module>()
     16     errD_sum +=errD
     17 
---> 18     errG, errL1 = netG_train([trainA, trainB])
     19     errG_sum += errG
     20     errL1_sum += errL1

~\AppData\Local\conda\conda\envs\venv\lib\site-packages\keras-2.1.2-py3.5.egg\keras\backend\tensorflow_backend.py in __call__(self, inputs)
   2466         session = get_session()
   2467         updated = session.run(fetches=fetches, feed_dict=feed_dict,
-> 2468                               **self.session_kwargs)
   2469         return updated[:len(self.outputs)]
   2470 

~\AppData\Local\conda\conda\envs\venv\lib\site-packages\tensorflow\python\client\session.py in run(self, fetches, feed_dict, options, run_metadata)
    776     try:
    777       result = self._run(None, fetches, feed_dict, options_ptr,
--> 778                          run_metadata_ptr)
    779       if run_metadata:
    780         proto_data = tf_session.TF_GetBuffer(run_metadata_ptr)

~\AppData\Local\conda\conda\envs\venv\lib\site-packages\tensorflow\python\client\session.py in _run(self, handle, fetches, feed_dict, options, run_metadata)
    980     if final_fetches or final_targets:
    981       results = self._do_run(handle, final_targets, final_fetches,
--> 982                              feed_dict_string, options, run_metadata)
    983     else:
    984       results = []

~\AppData\Local\conda\conda\envs\venv\lib\site-packages\tensorflow\python\client\session.py in _do_run(self, handle, target_list, fetch_list, feed_dict, options, run_metadata)
   1030     if handle is None:
   1031       return self._do_call(_run_fn, self._session, feed_dict, fetch_list,
-> 1032                            target_list, options, run_metadata)
   1033     else:
   1034       return self._do_call(_prun_fn, self._session, handle, feed_dict,

~\AppData\Local\conda\conda\envs\venv\lib\site-packages\tensorflow\python\client\session.py in _do_call(self, fn, *args)
   1037   def _do_call(self, fn, *args):
   1038     try:
-> 1039       return fn(*args)
   1040     except errors.OpError as e:
   1041       message = compat.as_text(e.message)

~\AppData\Local\conda\conda\envs\venv\lib\site-packages\tensorflow\python\client\session.py in _run_fn(session, feed_dict, fetch_list, target_list, options, run_metadata)
   1019         return tf_session.TF_Run(session, options,
   1020                                  feed_dict, fetch_list, target_list,
-> 1021                                  status, run_metadata)
   1022 
   1023     def _prun_fn(session, handle, feed_dict, fetch_list):

KeyboardInterrupt: 
In [63]:
print(len(patch_B))
16
In [24]:
_, valA, valB = val_batch.send(6) 
fakeB = netG_gen(valA)
showX(np.concatenate([valA, valB, fakeB], axis=0), 3)
errL1_sum = errG_sum = errD_sum = 0
_, valA, valB = next(val_batch)
fakeB = netG_gen(valA)
showX(np.concatenate([valA, valB, fakeB], axis=0), 3)
IOPub data rate exceeded.
The notebook server will temporarily stop sending output
to the client in order to avoid crashing it.
To change this limit, set the config variable
`--NotebookApp.iopub_data_rate_limit`.
In [25]:
def read_single_image(fn):
    
    im = Image.open(fn)
    im = im.resize( (loadSize, loadSize), Image.BILINEAR )
    arr = np.array(im)/255.0*2-1
    w1,w2 = (loadSize-imageSize)//2,(loadSize+imageSize)//2
    h1,h2 = w1,w2
    img = arr[h1:h2, w1:w2, :]
    
    return img
In [26]:
# 0~636

max_idx = 636
src_dir = './cards_ab/test_in/'
dst_dir = './cards_ab/test_out/'

for idx in range(max_idx+1):
    
    data = []
    
    src_path = src_dir + str(idx) + '.jpg'
    dst_path = dst_dir + str(idx) + '.jpg'
    
    src_img = read_single_image(src_path)
    data.append(src_img)
    data = np.float32(data)
        
    fake = netG_gen(data)

    int_X = ( (fake[0]+1)/2.0*255).clip(0,255).astype('uint8')
    int_X = int_X.reshape(-1,imageSize,imageSize, 3)
    int_X = int_X.reshape(1, -1, imageSize, imageSize,3).swapaxes(1,2).reshape(1*imageSize,-1, 3)

    dst_img = Image.fromarray(int_X)
    dst_img.save(dst_path, quality=90)
In [31]:
a = [1,23,4,5]
np.mean(a)
Out[31]:
8.25
In [27]:
netD.save_weights("Discriminator.h5")
netG.save_weights("Generator.h5")
In [ ]: